# -- coding: utf-8 --
from wnboss05.tools.util import Base, DBParser


class Reporter:

    @classmethod
    def generate_html_report(cls, dbname, table_name, version):
        """
        生成规定模板的测试报告
        :param table: 结果表的表名
        :param version: 版本号
        :return: None
        """
        # 读取模板内容
        with open('template.html', encoding='utf8') as file:
            contents = file.read()
        contents = contents.replace('$name', table_name)

        # 获取报告生成的日期和时间
        report_time = Base.get_time("%Y/%m/%d %H:%M:%S")
        contents = contents.replace('$report-date', report_time.split(' ')[0])
        contents = contents.replace('$last-time', report_time.split(' ')[1])

        # 更新版本
        contents = contents.replace('$test-version', version)

        # 获得测试用例执行通过、失败和错误的数据
        sql1 = f'select count(tid) from {table_name} where version="{version}" and result="test pass"'
        test_pass_result = str(DBParser.query(dbname, sql1)[0][0])
        sql2 = f'select count(tid) from {table_name} where version="{version}" and result="test fail"'
        test_fail_result = str(DBParser.query(dbname, sql2)[0][0])
        sql3 = f'select count(tid) from {table_name} where version="{version}" and result="test error"'
        test_error_result = str(DBParser.query(dbname, sql3)[0][0])

        # 将以上的三项结果进行替换
        contents = contents.replace('$pass-count', test_pass_result)
        contents = contents.replace('$fail-count', test_fail_result)
        contents = contents.replace('$error-count', test_error_result)

        # 测试数据的结果
        sql = f'select * from {table_name} where version="{version}"'
        result = DBParser.query(dbname, sql)
        test_result = ''
        for record in result:
            if record[9] == 'test pass':
                color = 'lightgreen'
            elif record[9] == 'test fail':
                color = 'red'
            else:
                color = 'yellow'
            screenshot = ''
            if record[9] != 'test pass' and record[3] == 'ui':
                screenshot = f'<a href="{record[-1]}">查看截图</a>'
            else:
                screenshot = '无'
            test_result += f'<tr height="40">' \
                           f'<td width="7%">{record[0]}</td>' \
                           f'<td width="9%">{record[2]}</td>' \
                           f'<td width="7%">{record[3]}</td>' \
                           f'<td width="7%">{record[4]}</td>' \
                           f'<td width="10%">{record[5]}</td>' \
                           f'<td width="7%" bgcolor="{color}">{record[9]}</td>' \
                           f'<td width="15%">{record[10]}</td>' \
                           f'<td width="15%">{record[11]}</td>' \
                           f'<td width="10%">{screenshot}</td>' \
                           f'</tr>\r\n'
        contents = contents.replace('$test-result', test_result)

        # 生成报告的路径及名称
        Base.create_dir(f'{version}')
        report_name = f'./{version}/{table_name}_{Base.get_time("%Y%m%d_%H%M%S")}.html'     # woniuboss_20230306_104030.html

        # 将内容写入报告中
        with open(report_name, 'w', encoding='utf8') as file:
            file.write(contents)


if __name__ == '__main__':
    Reporter.generate_html_report('testresult', 'woniuboss', 'v4.1')